org  07c00h
StackStart  equ 07c00h
LoaderStart  equ 09000h
LoaderOffset  equ 0100h
SectorsRoot  equ 14
RootDirectoryFirst equ 19
FAT1First  equ 1
DeltaSector  equ 17 
jmp short STARTMAIN
nop
OEMName DB 'Gensh...'
SectorSize DW 512
SectorClus DB 1
ReservedSector DW 1
FATNum DB 2
RootCNT DW 224
TOT_SEC16 DW 2880
Media DB 0xF0
FatSize16 DW 9
TrkSec DW 18
HeadNum DW 2
SecHidden DD 0
TOT_SEC32 DD 0
DriveNum DB 0
Reserved1 DB 0
BootSig DB 29h
VolID DD 0
VolLabel DB 'Ge...Impact'
FileSystemType DB 'FAT12   '
STARTMAIN: 
     mov ax,cs
     mov ds,ax
     mov es,ax
     mov ss,ax
     mov sp,StackStart
     mov ax,0600h
     mov bx,0700h
     mov cx,0
     mov dx,0184fh
     int 10h
     mov dh,0
     call Display
     xor ah,ah
     xor dl,dl
     int 13h
     mov word [SectorNumw],RootDirectoryFirst
SEARCH_ROOT:
     cmp word [RootSizew],0
     jz NOT_FOUND
     dec word [RootSizew]
     mov ax,LoaderStart
     mov es,ax
     mov bx,LoaderOffset
     mov ax,[SectorNumw]
     mov cl,1
     call ReadSector
     mov si,LoaderFileName
     mov di,LoaderOffset
     cld
     mov dx,10h
SEARCH_LOADERBIN:
     cmp dx,0
     jz GO_TO_NEXT
     dec dx
     mov cx,11
FILENAMECMP:
     cmp cx,0
     jz FOUNT_LOADER
     dec cx
     lodsb
     cmp al,byte [es:di]
     jz GO_ON
     jmp DIFFERENT
GO_ON:
     inc di
     jmp FILENAMECMP
DIFFERENT:
     and di,0FFE0h
     add di,20h
     mov si,LoaderFileName
     jmp SEARCH_LOADERBIN
GO_TO_NEXT:
     add word [SectorNumw],1
     jmp SEARCH_ROOT
NOT_FOUND:
     mov dh,2
     call Display
     jmp $
FOUNT_LOADER:
     mov ax,SectorsRoot
     and di,0FFE0h
     add di,01Ah
     mov cx,word [es:di]
     push cx
     add cx,ax
     add cx,DeltaSector
     mov ax,LoaderStart
     mov es,ax
     mov bx,LoaderOffset
     mov ax,cx
GOON_LOADING_FILE:
     push ax
     push bx
     mov ah,0Eh
     mov al,'.'
     mov bl,0Fh
     int 10h
     pop bx
     pop ax
     mov cl,1
     call ReadSector
     pop ax
     call GetFATEntry
     cmp ax,0FFFh
     jz FILE_LOADED
     push ax
     mov dx,SectorsRoot
     add ax,dx
     add ax,DeltaSector
     add bx,[SectorSize]
     jmp GOON_LOADING_FILE
FILE_LOADED:
     mov dh,1
     call Display
RootSizew dw SectorsRoot
SectorNumw  dw 0
bOdd   db 0
LoaderFileName  db "LOADER.BIN",0
LenSTR  equ 23
BootMessage:  db "Starting Genshin_Impact"
Message1  db "OK!                    "
Message2  db "No Loader Found        "
Display:
     mov ax,LenSTR
     mul dh
     add ax,BootMessage
     mov bp,ax
     mov ax,ds
     mov es,ax
     mov cx,LenSTR
     mov ax,01301h
     mov bx,0007h
     mov dl,0
     int 10h
     ret
ReadSector:
     push bp
     mov bp,sp
     sub esp,2
     mov byte [bp-2],cl
     push bx
     mov bl,[TrkSec]
     div bl
     inc ah
     mov cl,ah
     mov dh,al
     shr al,1
     mov ch,al
     and dh,1
     pop bx
     mov dl,[DriveNum]
.GoOnReading:
     mov ah,2
     mov al,byte [bp-2]
     int 13h
     jc .GoOnReading
     add esp,2
     pop bp
     ret
GetFATEntry:
     push es
     push bx
     push ax
     mov ax,LoaderStart
     sub ax,0100h
     mov es,ax
     pop ax
     mov byte [bOdd],0
     mov bx,3
     mul bx
     mov bx,2
     div bx
     cmp dx,0
     jz IS_EVEN
     mov byte [bOdd],1
IS_EVEN:
     xor dx,dx   
     mov bx,[SectorSize]
     div bx
     push dx
     mov bx,0
     add ax,FAT1First
     mov cl,2
     call ReadSector
     pop dx
     add bx,dx
     mov ax,[es:bx]
     cmp byte [bOdd],1
     jnz IS_EVEN_2
     shr ax,4
IS_EVEN_2:
     and ax,0FFFh
GET_FAT_OK:
     pop bx
     pop es
     ret
times  510-($-$$) db 0
dw  0xaa55